package com.xiaoma.myaudience.biz.util;

import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ListAdapter;
import android.widget.ListView;

public class ResetAbstractListHight {

	 /**
     * 测量listview实际高度
     * @param listView
     */
    public static void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();   
        if (listAdapter == null) {  
            // pre-condition  
            return;  
        } 
        
    	int totalHeight = 0;  
        for (int i = 0; i < listAdapter.getCount(); i ++) {
            View listItem = listAdapter.getView(i, null, listView);  
            listItem.measure(0, 0);  
            totalHeight += listItem.getMeasuredHeight();  
        }  
        
        ViewGroup.LayoutParams params = listView.getLayoutParams();  
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));  
        listView.setLayoutParams(params);  
        
    }  
    
    /**
     * 测量gridview实际高度
     * @param gridView
     */
    public static void setGridViewHeightBasedOnChildren(GridView gridView, int runnums) {
        ListAdapter listAdapter = gridView.getAdapter();   
        if (listAdapter == null) {  
            // pre-condition  
            return;  
        } 
        
    	int totalHeight = 0;  
        for (int i = 0; i < listAdapter.getCount(); i += runnums) { // gridview 需要以每行的个数增长
            View listItem = listAdapter.getView(i, null, gridView);  
            listItem.measure(0, 0);  
            totalHeight += listItem.getMeasuredHeight();  
        }  
        
        ViewGroup.LayoutParams params = gridView.getLayoutParams();  
        params.height = totalHeight;
        gridView.setLayoutParams(params);  
        
    }
    
}
